<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            text-align: center;
            line-height: 40px;
            border-radius: 10px;
            user-select: none;
        }
        .aa{
            width: 200px;
            height: 100px;
            position: fixed;
            top: 50%;
            left: 50%;
            margin-left: -100px;
            margin-top: -50px;
        }
        .asd{
            width: 40px;
            height: 40px;
            border: 1px solid red;
        }
        .up,.left,.right{
            position:absolute;
            bottom: 10px;
        }
        .left{
            left: -60px;
        }
        .right{
            left: 60px;
        }
    </style>
</head>
<body>
    <div class="aa">
        <div class="asd ArrowUp">上</div>
        <div class="asd up ArrowDown">下</div>
        <div class="asd left ArrowLeft">左</div>
        <div class="asd right ArrowRight">右</div>
    </div>
</body>
</html>
<script>
    const asd = document.querySelectorAll('.asd')
    document.addEventListener('keydown',keydown)
    function keydown(even){
        // asd.forEach(a => {
        //     if(a.classList.contains(even.key)){
        //         a.style.background = 'red';
        //     }
        // })
        document.querySelector(`.${even.key}`).style.background = 'red'
    }
    //随机颜色
    document.addEventListener('keyup',e => {
        asd.forEach(div => {
            div.style.background = 'white';
        })
    })
</script>